package info

import (
	"encoding/json"
	"reflect"
	"time"

	"github.com/yusufpapurcu/wmi"
	//"github.com/StackExchange/wmi"
)

type Win32_BaseBoard struct {
	Caption                 string    `json:"说明"`
	ConfigOptions           []string  `json:"配置数组"`
	CreationClassName       string    `json:"ClassName"`
	Depth                   float32   `json:"深度"`
	Description             string    `json:"描述"`
	Height                  float32   `json:"高度"`
	HostingBoard            bool      `json:"是否基板"`
	HotSwappable            bool      `json:"热交换"`
	InstallDate             time.Time `json:"安装时间"`
	Manufacturer            string    `json:"造商名称"`
	Model                   string
	Name                    string  `json:"名称"`
	OtherIdentifyingInfo    string  `json:"资产标记信息"`
	PartNumber              string  `json:"零件号"`
	PoweredOn               bool    `json:"是否上电"`
	Product                 string  `json:"基板部件号"`
	Removable               bool    `json:"可移动"`
	Replaceable             bool    `json:"可替换"`
	RequirementsDescription string  `json:"需求描述"`
	RequiresDaughterBoard   bool    `json:"辅助卡"`
	SerialNumber            string  `json:"序例号"`
	SKU                     string  `json:"库存单位号"`
	SlotLayout              string  `json:"其他相关"`
	SpecialRequirements     bool    `json:"特殊要求"`
	Status                  string  `json:"状态"`
	Tag                     string  `json:"标记"`
	Version                 string  `json:"版本"`
	Weight                  float32 `json:"重量"`
	Width                   float32 `json:"宽度"`
}

func BaseBoardInfo() (borad []Win32_BaseBoard, err error) {
	err1 := wmi.Query("Select * from Win32_BaseBoard", &borad)
	if err1 != nil {
		return nil, err1
	}

	return borad, nil
}

func JsonBaseBoardInfo() (f interface{}, err error) {
	board, err := BaseBoardInfo()
	if err != nil {
		return "", err
	}
	t, jsonerr := json.Marshal(board)
	if jsonerr != nil {
		return "", jsonerr
	}
	return t, nil

}
func MapBaseBoardInfo() (data []map[string]interface{}, err error) {

	board, err := BaseBoardInfo()
	if err != nil {
		return data, err
	}
	t, jsonerr := json.Marshal(board)
	if jsonerr != nil {
		return data, jsonerr
	}
	json.Unmarshal(t, &data)
	return data, nil

}

func JsonBaseBoardInfoEn() (f interface{}, err error) {
	boardinfo, err := BaseBoardInfo()
	if err != nil {
		return "", err
	}
	jsonData := make(map[string]interface{}, 0)
	jsonDataList := make([]map[string]interface{}, 0)
	for _, board := range boardinfo {
		bType := reflect.TypeOf(board)
		bValue := reflect.ValueOf(&board)
		for i := 0; i < bType.NumField(); i++ {
			field := bType.Field(i)
			jsonData[field.Name] = bValue.Elem().Field(i).Interface()
		}
		jsonDataList = append(jsonDataList, jsonData)
	}
	t, jsonErboardinfo := json.Marshal(jsonDataList)
	if jsonErboardinfo != nil {
		return "", jsonErboardinfo
	}
	return t, nil
}
